package personal.zq.practice.common.utils.leetcode.medium;

/**
 * 找树左下角的值
 *
 * @author zhangqing
 * @version 1.0
 * @date 2022/6/22
 */
public class FindBottomLeftValue {
    public static void main(String[] args) {
        //        TreeNode root= new TreeNode(1);

    }

    int curVal = 0;
    int curHeight = 0;

    public int findBottomLeftValue(TreeNode root) {
        int curHeight = 0;
        dfs(root, 0);
        return curVal;
    }

    public void dfs(TreeNode root, int height) {
        if (root == null) {
            return;
        }
        height++;
        dfs(root.left, height);
        dfs(root.right, height);
        if (height > curHeight) {
            curHeight = height;
            curVal = root.val;
        }
    }

    private class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
